import java.util.StringTokenizer;

class P712 {

	public static void main(String[] args) {
				
		int blablas = 1;
		
		while (true) {
			int n = Integer.parseInt(readLn());
			if (n == 0) break;
			
			int[] array = new int[(int)Math.pow(2, (n +1)) - 1];
			
			int[] map = new int[n+1];
			
			
			StringTokenizer st = new StringTokenizer(readLn());
			
			for (int k = 1; k <= n; k++) {
				String token = st.nextToken();
				map[k] = Integer.parseInt(token.substring(1));
			}
			
			String line = readLn();
			int index = array.length - 1;
			for (int k = line.length() - 1; k >=0; k--) {
				array[index--] = Integer.parseInt(line.charAt(k)+"");
			}
			
//			System.out.println(".....");
//			for (int t = 0; t < array.length; t++) {
//				System.out.print(array[t]+ " ");
//			}
//			System.out.println("\n.....");

			
			String resultado = "";
			
			int cases = Integer.parseInt(readLn());
			for (int k = 0; k < cases; k++) {
				line = readLn();
				int[] variables = new int[n+1];
//				for (int l = 0; l < line.length(); l++) {
//					int x = Integer.parseInt(line.charAt(l)+"");
//					variables[l+1] = x;
//				}
				
				for (int i = 1; i < map.length; i++) {
					variables[i] = Integer.parseInt(""+line.charAt(map[i] - 1));
				}
				
//				System.out.println(".....");
//				for (int t = 0; t < variables.length; t++) {
//					System.out.print(variables[t]+ " ");
//				}
//				System.out.println("\n.....");
				
				int indexTree = 1;
				
				for (int i = 1; i <=n ; i++) {
					if (variables[i] == 0) {
						indexTree = (indexTree * 2) + 0; 
					} else {
						indexTree = (indexTree * 2) + 1;
					}
				}
				
//				System.out.println(indexTree);
				
				resultado += (array[indexTree-1]);
			}
			
			System.out.println("S-Tree #" + blablas + ":");
			System.out.println(resultado);
			System.out.println();
			blablas++;
			
		}
		
	}
	
	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}
}
